package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    @RequireLogin
    @RequestMapping("/get")
    @ResponseBody
    public Object get(Long id){
        return userInfoService.get(id);
    }

    // 手机号码唯一性校验
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    // 发送验证码
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    // 用户注册
    /*phone: 13700000003
    nickname: 山下智久
    password: 1
    rpassword: 1
    verifyCode: f1c5*/
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode){
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return AjaxResult.SUCCESS;
    }

    // 用户登陆
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse resp){
        String token = userInfoService.userLogin(username, password);
        // 8: 创建cookie对象 Cookie c = new Cookie("token", 创建出来的随机码) 注意: 设置cookie存活时间30分钟
        CookieUtil.addCookie("token", token, RedisKeys.USER_LOGIN_TOKEN.getTime().intValue(), resp);
        return AjaxResult.SUCCESS;
    }
}
